package com.example.androidlistsorter;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private final static int EXPONENT_OF_RANDOM_COUNT = 5;

	private EditText edtMinRandom;
	private EditText edtMaxRandom;
	private NumberPicker edtRandomCount;
	private EditText edtConsole;
	
	private Button btnRandomFileCreate;
	private Button btnSort;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		edtMinRandom = (EditText) this.findViewById(R.id.edtMinRandom);
		edtMaxRandom = (EditText) this.findViewById(R.id.edtMaxRandom);
		edtRandomCount = (NumberPicker) this.findViewById(R.id.edtRandomCount);
		String[] randomCountValues = randomCountStringArray(EXPONENT_OF_RANDOM_COUNT);
		edtRandomCount.setDisplayedValues(randomCountValues);
		edtRandomCount.setMaxValue(randomCountValues.length-1);
		edtRandomCount.setMinValue(0);
		
		edtConsole = (EditText) this.findViewById(R.id.edtConsole);
		
		btnRandomFileCreate = (Button) this.findViewById(R.id.btnRandomFileCreate);
		btnRandomFileCreate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				int arraySize = Integer.valueOf(edtRandomCount.getDisplayedValues()[edtRandomCount.getValue()]);
				int minRandom = Integer.valueOf(edtMinRandom.getText().toString());
				int maxRandom = Integer.valueOf(edtMaxRandom.getText().toString());
				String intsString = RandomIntString(minRandom, maxRandom, arraySize);
				
				BufferedWriter writer = null;
				try
				{
					writer = new BufferedWriter(new FileWriter(getExternalFilesDir(null)+"//randoms.txt"));
					writer.write(intsString);
					Toast.makeText(getApplicationContext(), "randoms.txt file created", Toast.LENGTH_SHORT).show();
				}
				catch ( IOException exception)
				{
					Toast.makeText(getApplicationContext(), "Can't create randoms.txt file", Toast.LENGTH_LONG).show();
				}
				finally
				{
					try
					{
						if ( writer != null)
							writer.close( );
					}
					catch ( IOException exception)
					{
					}
			     }
			}			
		});
		
		btnSort = (Button) this.findViewById(R.id.btnSort);
		btnSort.setFocusableInTouchMode(true);
		btnSort.requestFocus();
		
		btnSort.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SortingAsyncTask sortingTask = new SortingAsyncTask(MainActivity.this, getExternalFilesDir(null).getAbsolutePath() , edtConsole);
				sortingTask.execute(getExternalFilesDir(null).getAbsolutePath());
			}
		});
	}
	
	private static String[] randomCountStringArray(int maxExponent) {
		String[] s = new String[maxExponent*9];
		for (int exponent = 0, k = 0 ; exponent <= maxExponent-1; exponent++)
			for (int i = 1 ; i <= 9; i++, k++)
				s[k] = ""+(int) (i*Math.pow(10, exponent));
		return s;
	}
	
	private static String RandomIntString (int min, int max, int count) {
		Random random = new Random();
		StringBuilder outString = new StringBuilder("");
		int range = max - min + 1;
		for (int i = 1; i <= count; i++)
			outString.append( (random.nextInt(range) + min) + "\n");
		return outString.toString().trim();
	}
	
}
